define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'meter/log/index',
                    add_url: 'meter/log/add',
                    edit_url: 'meter/log/edit',
                    del_url: 'meter/log/del',
                    multi_url: 'meter/log/multi',
                    import_url: 'meter/log/import',
                    table: 'meter_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },

                        {

                            field: 'meter_id',

                            title: __('Meter_id'),

                            formatter: function(meter_id, rows) {
                                var meter = rows.meter;
                                if (meter) {
                                    return meter.name;
                                }
                                return null;
                            }
                        },
                        { field: 'value', title: __('Value') },

                        { field: 'update_time', title: __('Update_time'), formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('#toolbar').on('click', '.btn-import', function(e) {
                e.stopPropagation();
                var options = table.bootstrapTable('getOptions');
                Fast.api.open(options.extend.import_url, __('Batch Import'))
            })
        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        import: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});